package com.evil.common.application.enums.form;

import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Optional;

/**
 * 控件默认内容类型
 *
 * @author liyang
 * @date 2022-08-08 00:00
 */
@AllArgsConstructor
public enum DefaultValueTypeEnum {
    /**
     * 默认内容类型(0:自定义, 1:关联应用数据, 2: 通过公式运算, 3: 关联已有表格)
     */
    DIY(0, "自定义"),
    RELATED_FORM(1, "关联应用数据"),
    FORMULA_CALCULATION(2, "通过公式运算"),
    RELATED_TABLE(3, "关联已有表格"),
    ;

    @Getter
    private final int id;
    @Getter
    private final String name;

    public static Optional<DefaultValueTypeEnum> findById(Integer id) {
        if (null == id) {
            return Optional.empty();
        }
        return Arrays.stream(values()).filter(e -> e.id == id).findFirst();
    }

    public static DefaultValueTypeEnum isExistById(Integer id) {
        return DefaultValueTypeEnum.findById(id).orElseThrow(() ->
                new BusinessException(RCodeEnum.ERROR_PARAMETER, "默认内容类型参数错误"));
    }
}
